<?php

namespace app\admin\controller;

use think\facade\Config;
use think\facade\Cache;
use app\admin\controller\Base;
use app\admin\model\AuthRule as AuthRuleModel;
class ManagerRule extends Base{
  public function initialize(){
    parent::initialize();
    $this->Model = new AuthRuleModel();
    $this->rule_type = 'manager';
  }

  function index(){
    $param = [];
    $list = $this->Model->explandWithDescendants($this->rule_type, [], 0);
    $this->assign('param', $param);
    $this->assign('list', $list);
    return $this->fetch();
  }

  public function create(){
    if($this->request->isPost()){
      $data = $this->getPostData();
      $save_res = $this->Model->saveItem($data);
      if($save_res['status']){
        $this->clear_menu_cache();
        $this->success($save_res['msg'], 'index');
      }else{
        $this->error($save_res['msg']);
      }
    }else{
      $list = $this->Model->explandWithDescendants($this->rule_type, [], 0);
      $this->assign('list', $list);
      $this->assign('rule', ['status' => true, 'parent_id' => 0]);
      return $this->fetch('form');
    }
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id){
    $this->redirect('index');
  }

  /**
   * 显示编辑资源表单页.
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function edit($id){
    if($this->request->isPost()){
      $data = $this->getPostData();
      $data['id'] = $id;
      $save_res = $this->Model->saveItem($data);
      if($save_res['status']){
        $this->clear_menu_cache();
        $this->success($save_res['msg'], 'index');
      }else{
        $this->error($save_res['msg']);
      }
    }else{
      $rule = $this->Model->where('id', $id)->find();
      if(empty($rule)){
        if($this->request->isAjax()){
          $this->error('访问页面不存在', 'index');
        }else{
          $this->redirect('index');
        }
      }

      $list = $this->Model->explandWithDescendants($this->rule_type, [], 0);
      $this->assign('list', $list);
      $this->assign('rule', $rule);
      return $this->fetch('form');
    }
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id){
    if($this->request->isDelete()){
      $del_res = $this->Model->deleteItem($id);
      if($del_res['status']){
        $this->clear_menu_cache();
        $this->success($del_res['msg'], 'index');
      }else{
        $this->error($del_res['msg']);
      }
    }else{
      $this->error('访问页面不存在', 'index');
    }
  }

  public function resort(){
    $param = $this->request->param();
    $items = $param['items'];
    foreach ($items as $id => $sort_num) {
      $sort_num = intval($sort_num);
      if($sort_num < 0){
        $sort_num = 0;
      }else if($sort_num >= 9999){
        $sort_num = 9999;
      }
      $this->Model->save(['sort' => $sort_num], ['id' => $id]);
    }
    $this->redirect('index');
  }

  private function getPostData(){
    $fields = [
      ['name' => 'id', 'type' => 'integer', 'default' => '', 'is_null' => true],
      ['name' => 'parent_id', 'type' => 'integer', 'default' => 0, 'is_null' => false],
      ['name' => 'title', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'name', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'icon', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'status', 'type' => 'boolean', 'default' => false, 'is_null' => false],
      ['name' => 'sort', 'type' => 'integer', 'default' => 100, 'is_null' => false],
      ['name' => 'tips', 'type' => 'string', 'default' => '', 'is_null' => false],
    ];
    $data = $this->parsePostParam($fields);
    $default_data = ['rule_type' => $this->rule_type];
    return array_merge($data, $default_data);
  }

  private function clear_menu_cache(){
    $key_names = ['rule_menu_tree_name', 'rule_menu_expland_name'];
    foreach ($key_names as $name) {
      Cache::rm($name);
    }
  }
}
